package com.sxt.dao;

import com.sxt.common.PageInfo;
import com.sxt.pojo.Customer;

import java.util.Map;

/**
 * @Description:
 * @author: Mr.T
 * @date 2020-11-04 14:35
 */
public class CustomerDao extends BaseDao {


    /**
     * 分页查询数据
     * @param params
     * @param page
     * @param limit
     * @return
     */
    public PageInfo<Customer> selectPage(Map<String,String> params, String page, String limit){
        String sql = "select c.`id`,c. `name`, c.`phone`, c.`sex`, c.`company`, c.`deleted`, c.`user_id`  as userId ,  c.`create_time` as createTime, c.`update_time` as updateTime, c.`delete_time` as deleteTime ,u.realname as realname from customer c LEFT JOIN user u on c.user_id = u.id  where 1=1  ";
        if (params.containsKey("name")){
            sql = sql + " and c.name like '%"+params.get("name")+"%' ";
        }
        if (params.containsKey("phone")){
            sql = sql + " and c.phone = '" +params.get("phone")+"'";
        }
        if (params.containsKey("sex")){
            sql = sql + " and c.sex = '" +params.get("sex")+"'";
        }
        if (params.containsKey("company")){
            sql = sql + " and c.company like '%"+params.get("company")+"%' ";
        }
        if (params.containsKey("deleted")){
            sql = sql + " and c.deleted = '" +params.get("deleted")+"'";
        }
        if (params.containsKey("userId")){
            sql = sql + " and c.user_id = '" +params.get("userId")+"'";
        }
        PageInfo<Customer> pageInfo = super.selectPage(sql, Customer.class, page, limit);
        return  pageInfo;
    }

    /**
     * 根据手机号查询客户
     * @param phone
     * @return
     */
    public Customer selectCustomerByPhone(String phone) {
        String sql = "select `id` from customer where phone = ? ";
        return  super.selectOne(sql,Customer.class,phone);
    }

    /**
     * 新增一个客户
     * @param name
     * @param phone
     * @param sex
     * @param company
     * @param id
     */
    public void insert(String name, String phone, String sex, String company, Integer id) {
        String sql = "insert into customer (name,phone,sex,company,user_id) value(?,?,?,?,?)";
        super.update(sql,name,phone,sex,company,id);
    }

    /**
     * 修改客户信息
     * @param id
     * @param name
     * @param phone
     * @param sex
     * @param company
     */
    public void update(String id,String name, String phone, String sex, String company) {
        String sql = "update customer set name = ?,phone=?,sex=?,company=? where id=?";
        super.update(sql,name,phone,sex,company,id);
    }

    /**
     * 修改客户的状态
     * @param id
     * @param deleted
     */
    public void updateDateted(String id, Integer deleted) {
        String sql = "update customer set deleted = ? ,delete_time = now() where id=?";
        super.update(sql,deleted,id);
    }

    /**
     * 批量的修改客户的业务员ID
     * @param userId
     * @param cIds
     */
    public void batchUpdateSalemanId(String userId, String[] cIds) {
       String sql = "update  customer  set user_id = ? where id in ( ";
        for (String cId : cIds) {
            sql = sql + cId + ",";
        }
        //使用不存在的ID 0  规避最后一个,号的问题  或者 去掉最后一个逗号
        sql = sql + "0)";
        //批量更新
        super.update(sql,userId);
    }
}
